package com.cx.mall.order.controller;

import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.common.model.order.OrderInfo;
import com.cx.mall.order.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author lyt
 * @since 2025-07-14
 */
@RestController
@RequestMapping("order/orderInfo")
public class OrderInfoController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @GetMapping("/user/{id}")
    public ResponseVO listByUserId(@PathVariable Long id){
        List<OrderInfo> list = orderInfoService.listByUserId(id);
        return ResponseVO.success(list);
    }

    @GetMapping("/orderState/{status}")
    public ResponseVO listByOrderState(@PathVariable Long id ,@PathVariable Integer status){
        List<OrderInfo> list = orderInfoService.listByOrderState(id ,status);
        return ResponseVO.success(list);
    }

    @PostMapping("/save")
    public ResponseVO save(OrderInfo orderInfo){
        orderInfoService.save(orderInfo);
        return ResponseVO.success();
    }

    /**
     * 修改订单
     * @param orderInfo
     * @return
     */
    @PutMapping("/update")
    public ResponseVO update(@RequestBody OrderInfo orderInfo){
        orderInfoService.updateById(orderInfo);
        return ResponseVO.success();
    }

    /**
     * 查询订单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseVO getById(@PathVariable String id){
        OrderInfo orderInfo = orderInfoService.getById(id);
        return ResponseVO.success(orderInfo);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/delete")
    public ResponseVO<Boolean> deleteById(@RequestParam("orderId") Long orderId) {
        boolean success = orderInfoService.removeById(orderId);
        return ResponseVO.success(success);
    }

    /**
     * 更新订单状态
     */
    @PostMapping("/update/status")
    public ResponseVO<Boolean> updateStatus(
            @RequestParam("orderId") Long orderId,
            @RequestParam("status") Integer status) {
        boolean success = orderInfoService.updateStatus(orderId, status);
        return ResponseVO.success(success);
    }

}
